package com.yao.sdk.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class ExtGridLayoutManager extends GridLayoutManager{
    // RecyclerView高度随Item自适应


    public ExtGridLayoutManager(Context context,int spanCount) {
        super(context,spanCount);
    }
    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, final int widthSpec,final int heightSpec) {
        int itemCount = state.getItemCount();
        int spnCount = getSpanCount();
        int i = itemCount % spnCount;
        int lineCount = itemCount / spnCount;
        if (i > 0 ) {
            lineCount += 1;
        }
        if (itemCount < spnCount) {
            lineCount = 1;
        }
        if (lineCount < 1) {
            lineCount = 1;
        }

        View view = recycler.getViewForPosition(0);
        if(view != null){
            measureChild(view, widthSpec, heightSpec);
            int measuredWidth = View.MeasureSpec.getSize(widthSpec);
            int measuredHeight = view.getMeasuredHeight();
            setMeasuredDimension(measuredWidth, measuredHeight*lineCount);
        }
    }

}